using UnityEngine;
using XMLData.Map;
using XMLReaderWriter;
using System.IO;
using System.Collections.Generic;

class Notification
{
	List<Notification> notificationParent;
	LevelBuilderGUI.EmptyVoid notifCallBack;
	string message;
	
	public Notification(string message, LevelBuilderGUI.EmptyVoid callback, List<Notification> notificationParent)
	{
		this.message = message;
		this.notifCallBack = callback;
		this.notificationParent = notificationParent;
	}
	
	public void ShowNotif()
	{
		GUILayout.BeginArea(new Rect(Screen.width/3, Screen.height/3, Screen.width/2, Screen.height/6));
		GUILayout.BeginHorizontal();
		GUILayout.FlexibleSpace();
		GUILayout.TextArea(message);
		GUILayout.FlexibleSpace();
		GUILayout.EndHorizontal();
		
		GUILayout.BeginHorizontal();
		if(GUILayout.Button("Confirm"))
		{
			notifCallBack();
			removeFromList();
		}
		
		if(GUILayout.Button("Cancel"))
			removeFromList();
		
		GUILayout.EndHorizontal();
		GUILayout.EndArea();	
	}
	
	protected void removeFromList()
	{
		notificationParent.Remove(this);
	}
}

[RequireComponent(typeof(LevelManager))]
[RequireComponent(typeof(LevelAssetManager))]
public class LevelBuilderGUI : MonoBehaviour {
	
	public delegate void EmptyVoid();
	
	/// <summary>
	/// The current button setup per world map creation state.
	/// </summary>
	EmptyVoid currentGUISetup;
	Stack<EmptyVoid> guiSetupStack;
	EmptyVoid[] transitionArray;
	const int stackLevel = 3;
	int currentSetup;
	float currentTime;
	bool isTransition = false;
	float myVal = 0.0f;
	
	//Map variables
	bool isMapExisting;
	List<string> mapList;
	string[] mapArray;
	int chosenMap = -1;
	
	//Vector2 with int
	GridSize levelGridSize;
	
	//Choosing what elements to put
	int chosenCategory = -1;
	int subChosen = -1;
	string[] categories = {"Cell","Theme", "Obj", "Tut"};
	string[] themes = {"Cute"};
	Texture2D[] themeSnip;
	Texture2D[] objects;
	Texture2D[] cells; //Elements available for cell category
	Texture2D[] bgs; //Background element available
	Texture2D[] tuts;
	EmptyVoid currentCategory;
	Vector2 scrollPos; //The scroll position of the category
	Vector2 currentGUIScroll = Vector3.zero;
	Vector2 elementNotifScroll = Vector3.zero; 
	
	//Shows the element that can be reduced
	ILevelElementGUI elementNotif = null;
	bool isViewedSeparetely;
	EmptyVoid previousView;
	Camera extraCam;
	public Camera cam;
	
	string nextMap = "";
	public GUISkin levelBuilderSkin;
	public Texture2D darkMask;
	
	bool isViewed;
	int hideOffSet;
	public GUIStyle selectionButtonStyle;

	public Camera nguiCamera;
	bool isExiting = false;
	int previousMask;

	//List<Notification> notifications;
	
	#region Initialize Level Builder
	void Start () 
	{ 	
		nguiCamera.cullingMask = 0;
		nguiCamera.cullingMask = 1 << LayerMask.NameToLayer("LevelEditUI");

		isViewed = true;
		//notifications = new List<Notification>();
		guiSetupStack = new Stack<EmptyVoid>(stackLevel);
		transitionArray = new EmptyVoid[2];
		currentTime = Time.time;
		
		themeSnip = new Texture2D[themes.Length];
		LevelAssetManager lam = LevelAssetManager.Instance;
		
		LevelAssetManager.Instance.loadAll();
		
		for(int i  = 0; i < themes.Length; i++)
		{
			Texture2D[] images = lam.getAssets<Texture2D>(KEYTYPE.PARALLAX, themes[i].ToUpper());
			themeSnip[i] = Instantiate(images[1]) as Texture2D;
			TextureScale.Bilinear(themeSnip[i], 90,45);
		}
		
		Texture2D[] _cells = LevelAssetManager.Instance.GetAllAsset<Texture2D>(KEYTYPE.CELL);
		cells = new Texture2D[_cells.Length];
		
		//Scale the cell to appropriately fit the screen
		for(int i = 0; i < _cells.Length; i++)
		{
			Texture2D newtex = Instantiate(_cells[i]) as Texture2D;
			cells[i] = newtex;
			TextureScale.Bilinear(newtex,45,45);
		}
		
		Texture2D[] _bgs = LevelAssetManager.Instance.GetAllAsset<Texture2D>(KEYTYPE.BG);
		bgs = new Texture2D[_bgs.Length];
		
		//Scale the cell to appropriately fit the screen
		for(int i = 0; i < _bgs.Length; i++)
		{
			Texture2D newtex = Instantiate(_bgs[i]) as Texture2D;
			bgs[i] = newtex;
			TextureScale.Bilinear(newtex,45,45);
		}
		
		Texture2D[] _objs = LevelAssetManager.Instance.GetAllAsset<Texture2D>(KEYTYPE.OBJECTS);
		objects = new Texture2D[_objs.Length];
		
		//Scale the cell to appropriately fit the screen
		for(int i = 0; i < _objs.Length; i++)
		{
			Texture2D newtex = Instantiate(_objs[i]) as Texture2D;
			objects[i] = newtex;
			TextureScale.Bilinear(newtex,45,45);
		}
		
		Texture2D[] _tuts = LevelAssetManager.Instance.GetAllAsset<Texture2D>(KEYTYPE.TUTORIAL);
		tuts = new Texture2D[_tuts.Length];
		
		//Scale the cell to appropriately fit the screen
		for(int i = 0; i < _tuts.Length; i++)
		{
			Texture2D newtex = Instantiate(_tuts[i]) as Texture2D;
			tuts[i] = newtex;
			TextureScale.Bilinear(newtex,45,45);
		}
		
		currentCategory = gui_ScrollCellCategory;
		
	  	lookForLocalMap();
		Camera.main.cullingMask |= 1 << LayerMask.NameToLayer("Default");
		changeState(state_OnRootState);
		levelGridSize = new GridSize(10,10);
	}
	#endregion
	
	#region Utility map methods

	public void OnExit()
	{
		isExiting = !isExiting;
		if(isExiting)
			LevelAssetManager.Instance.ChosenCell = null;
	}

	bool isMapBeingOverriden(string mapName)
	{
		string path = Application.dataPath + "/Maps";
		
		if(Directory.Exists(path))
		{
			string[] array = Directory.GetFiles(path, "*.xml"); 
			//Get all available maps for the player
			for(int i = 0; i < array.Length; i++)
			{
				if(Path.GetFileName(array[i]) == mapName)
				{
					return true;
				}
			}
		}
		return false;
	}
	void lookForLocalMap()
	{
		//Where we want to save and load to and from  
		mapList = LevelManager.Instance.MapList = new List<string>();

		string path = Application.dataPath + "/Maps";
		if(Directory.Exists(path))
		{
			string[] array = Directory.GetFiles(path, "*.xml"); 

			//Get all available maps for the player
			for(int i = 0; i < array.Length; i++)
			{
				string mapName = Path.GetFileName(array[i]);
				
				//if(!LevelManager.isMapWritable(mapName))
				//	continue;
				
				mapList.Add(mapName);
			}
		}
		
		mapArray = mapList.ToArray();
		//Test if there is at least one map existing
		try
		{
			LevelManager.Instance.MapName = mapArray[0];
			isMapExisting = true;
		}
		catch(System.Exception)
		{
			LevelManager.Instance.MapName = "YourMapName.xml";
			isMapExisting = false;
		}
	}
	
	void previewBothWorld(bool isViewedBothWorld)
	{
		if(isViewedBothWorld && !isViewedSeparetely)
		{
			Camera newCam =  extraCam = Instantiate(cam) as Camera;
			Camera mainCam = Camera.main;
			newCam.transform.parent = Camera.main.transform;
			newCam.transform.localPosition = Vector3.zero;
			
			newCam.rect = new Rect(0,0,1, 0.495f);
			int[] viewableLayer = { 1 << LayerMask.NameToLayer("Player1Specific"), 
									1 << LayerMask.NameToLayer("Player2Specific"),
									1 << LayerMask.NameToLayer("bg1"),
									1 << LayerMask.NameToLayer("bg2"),
									1 << LayerMask.NameToLayer("Default")};
			newCam.cullingMask = viewableLayer[0] | viewableLayer[2] | viewableLayer[4];
			mainCam.rect = new Rect(0, 0.505f, 1, 0.5f);
			mainCam.cullingMask = viewableLayer[1] | viewableLayer[3] | viewableLayer[4];
			isViewedSeparetely = true;
			previousView = guiSetupStack.Peek();
			currentGUISetup = state_OnWorldPreview;
			elementNotif = null;
			LevelGrid.Instance.ActivateCellCollider(false);
		}
		
		else if(!isViewedBothWorld)
		{
			isViewedSeparetely = false;
			Camera.main.rect = new Rect(0,0, 1,1);
			currentGUISetup = previousView;
			LevelGrid.Instance.ActivateWorld();
			LevelGrid.Instance.ReprocessBG();
			Destroy(extraCam.gameObject);
			LevelGrid.Instance.ActivateCellCollider(true);
		}
		
	}
	void changeState(EmptyVoid newState)
	{
		if(guiSetupStack.Count < stackLevel)
		{
			if(guiSetupStack.Count > 0)
			startTransition(newState, guiSetupStack.Peek());
			guiSetupStack.Push(newState);
			currentGUISetup = guiSetupStack.Peek();
		}	
	}
	
	void revertState()
	{
		int stackCount = guiSetupStack.Count;
		if(stackCount > 1)
		{
			EmptyVoid pastSetup = guiSetupStack.Pop();
			currentGUISetup = guiSetupStack.Peek();
			if(guiSetupStack.Count > 0)
				startTransition(currentGUISetup, pastSetup);
		}
	}
	
	void startTransition(EmptyVoid _to, EmptyVoid _from)
	{
		isTransition = true;
		myVal = 0.0f;
		currentTime = Time.time;
		transitionArray[0] = _to;
		transitionArray[1] = _from;
	}
	
	void transition()
	{
		int index = 0;
		
		//To Setup
		GUI.color = new Color(myVal, myVal,myVal, myVal);
		GUILayout.BeginArea(new Rect(Screen.width >> 6, Screen.height >> 6, 
									 Screen.width * 0.3f * hideOffSet, Screen.height/2));
			transitionArray[index]();
		GUILayout.EndArea();
		
		//From setup
		index = 1;
		float black = 1.0f - myVal;
		GUI.color = new Color(black, black,black, black);
		GUILayout.BeginArea(new Rect(Screen.width >> 6, Screen.height >> 6, 
									  Screen.width * 0.3f * hideOffSet, Screen.height/2));
			transitionArray[index]();
		GUILayout.EndArea();
	}
	
	#endregion
	
	#region GUI setup
	/// <summary>
	/// Shows the file name of the chosen map
	/// </summary>
	void guiMapName()
	{
		GUILayout.BeginHorizontal();
		GUILayout.Box("MapName: ", GUILayout.ExpandWidth(false)); 
		GUILayout.Box(LevelManager.Instance.MapName);
		GUILayout.EndHorizontal();
	}
	
	/// <summary>
	/// GUI scroll of the chosen category
	/// </summary>
	void gui_ScrollCellCategory()
	{
		scrollPos =  GUILayout.BeginScrollView(scrollPos, GUILayout.Width(Screen.width * 0.27f), GUILayout.Height(Screen.height * 0.2f));
		subChosen = GUILayout.SelectionGrid(subChosen, cells,3, selectionButtonStyle);
		if(subChosen != -1)
		{
			string cellName = cells[subChosen].name;
			cellName = cellName.Substring(0, cellName.Length -7);
			
			LevelAssetManager.Instance.CurrentCell = cellName;
			subChosen = -1;
		}
		GUILayout.EndScrollView();
	}
	
	void gui_ScrollBGCategory()
	{
		for(int i = 0; i < 3; i++)
		{
			GUILayout.BeginHorizontal();
			if(GUILayout.Button("Layer" + (i + 1)))
			{
				LevelGrid.Instance.returnBGPlaneColor();
				BGPlane2D bgPlane = LevelGrid.Instance.addHighlightToBG(i);
				elementNotif = bgPlane;
			}
			GUILayout.EndHorizontal();		
		}
		scrollPos =  GUILayout.BeginScrollView(scrollPos/*, GUILayout.Width(Screen.width * 0.27f), GUILayout.Height(Screen.height * 0.2f)*/);
		subChosen = GUILayout.SelectionGrid(subChosen, bgs,3);
		if(subChosen != -1)
		{
			string textureName = bgs[subChosen].name;
			textureName = textureName.Substring(0, textureName.Length -7);
			Texture2D image = LevelAssetManager.Instance.getAsset<Texture2D>(KEYTYPE.BG, textureName);
			LevelGrid.Instance.AddBackground(image);
			subChosen = -1;
		}
		
		GUILayout.EndScrollView();
	}
	
	void gui_ScrollTutCategory()
	{
		scrollPos =  GUILayout.BeginScrollView(scrollPos, GUILayout.Width(Screen.width * 0.27f), GUILayout.Height(Screen.height * 0.2f));
		subChosen = GUILayout.SelectionGrid(subChosen, tuts, 3, selectionButtonStyle);
		
		if(subChosen != -1)
		{
			string textureName = tuts[subChosen].name;
			textureName = textureName.Substring(0, textureName.Length -7);
			LevelGrid.Instance.CreateTutorial(textureName);
			subChosen = -1;
		}
		GUILayout.EndScrollView();
	}
	
	void gui_ScrollObjectCategory()
	{
		scrollPos = GUILayout.BeginScrollView(scrollPos, GUILayout.Width(Screen.width * 0.27f), GUILayout.Height(Screen.height * 0.2f));
		subChosen = GUILayout.SelectionGrid(subChosen, objects, 3, selectionButtonStyle);
		if(subChosen != -1)
		{
			string objName = objects[subChosen].name;
			objName = objName.Substring(0, objName.Length -7);
			Texture2D image = LevelAssetManager.Instance.getAsset<Texture2D>(KEYTYPE.OBJECTS, objName);
			LevelGrid.Instance.CreateObject(image.name);
			subChosen = -1;
		}
		GUILayout.EndScrollView();
	}
	
	void gui_ScrollThemeCategory()
	{
		scrollPos = GUILayout.BeginScrollView(scrollPos, GUILayout.Width(Screen.width * 0.27f), GUILayout.Height(Screen.height * 0.2f));
		subChosen = GUILayout.SelectionGrid(subChosen, themeSnip, 2, selectionButtonStyle);
		if(subChosen != -1)
		{
			//LevelGrid.Instance.changeBackground(themes[subChosen]);
			LevelGrid.Instance.CreateBG(themes[subChosen]);
			subChosen = -1;
		}
		GUILayout.EndScrollView();
	}
	
	/// <summary>
	/// GUI slider for the map grid
	/// </summary>
	void gui_SlideMapGrid()
	{
		GUILayout.BeginHorizontal();
		GUILayout.Label("Horizontal: " + levelGridSize.X.ToString().PadLeft(2, '0'), GUILayout.ExpandWidth(false));
		levelGridSize.X = (int)GUILayout.HorizontalSlider(levelGridSize.X, 5, 99);
		GUILayout.EndHorizontal();
		
		GUILayout.BeginHorizontal();
		GUILayout.Label("Vertical:    " + levelGridSize.Y.ToString().PadLeft(2,'0'), GUILayout.ExpandWidth(false));
		levelGridSize.Y = (int)GUILayout.HorizontalSlider(levelGridSize.Y, 5, 99);
		GUILayout.EndHorizontal();
	}
		
	/// <summary>
	/// Package the in an xml file
	/// </summary>
	void gui_BtnExportMap()
	{
		if(GUILayout.Button("Export Map"))
		{
			removeElementNotif();
			currentGUISetup = state_OnExport;
		}
	}
	
	void state_OnExport()
	{
		GUILayout.BeginHorizontal();
		GUILayout.Box("Map Name:");
		LevelManager.Instance.MapName = GUILayout.TextField(LevelManager.Instance.MapName, 12);
		GUILayout.EndHorizontal();
		
		GUILayout.BeginHorizontal();
		GUILayout.Box("Next Map:");
		nextMap = GUILayout.TextField(nextMap, 12);
		GUILayout.EndHorizontal();
		
		GUILayout.BeginHorizontal();
		
		if(GUILayout.Button("Confirm"))
		{
			if(!isMapBeingOverriden(LevelManager.Instance.MapName))
			{
				LevelManager.Instance.WrapMap(nextMap);
				lookForLocalMap();
				LevelManager.Instance.MapName = DataScript.GetRecentMapExport();
				removeElementNotif();
			}
			else
			{
				currentGUISetup = state_OnOverWriteMap;
				Debug.Log("This Map is being OverWritten");
			}
		}
		
		if(GUILayout.Button("Back"))
		{
			currentGUISetup = state_OnViewMap;
		}
		
		GUILayout.EndHorizontal();
	}
	
	void state_OnOverWriteMap()
	{
		GUILayout.BeginHorizontal();
		GUILayout.FlexibleSpace();
		GUILayout.TextArea("Do you want to replace \n" + LevelManager.Instance.MapName + " ?");
		GUILayout.FlexibleSpace();
		GUILayout.EndHorizontal();
		
		GUILayout.BeginHorizontal();
		if(GUILayout.Button("Confirm"))
		{
			LevelManager.Instance.WrapMap(nextMap);
			lookForLocalMap();
			LevelManager.Instance.MapName = DataScript.GetRecentMapExport();
			removeElementNotif();
			currentGUISetup = state_OnExport;
		}
		
		if(GUILayout.Button("Cancel"))
		{
			currentGUISetup = state_OnExport;
		}
		
		GUILayout.EndHorizontal();
	}
	/// <summary>
	/// Change the state of the game
	/// </summary>
	/// <param name='message'>
	/// GUI content string to put .
	/// </param>
	/// <param name='guiSetup'>
	/// GUI setup ot state used.
	/// </param>
	void gui_BtnStateChange(string message, EmptyVoid guiSetup)
	{
		if(GUILayout.Button(message))
		{
			if(guiSetup == state_OnCreateMap)
				LevelManager.Instance.MapName = "tmpMapName";
			
			changeState(guiSetup);
		}
	}
	
	public void clickCreateMap()
	{
		currentGUISetup  = state_OnCreateMap;
	}
	
	public void clickLoadMap()
	{
		currentGUISetup = state_OnLoadMap;
	}
	/// <summary>
	/// Loads the map on the screen
	/// </summary>
	void gui_BtnLoadMap()
	{
		if(GUILayout.Button("Load Map"))
		{
			Camera.main.GetComponent<EditorCamera>().enabled = true;
			LevelManager.Instance.LoadMap();
			LevelGrid.Instance.ActivateObjectCollider(false);
			changeState(state_OnViewMap);
		}
	}
	
	/// <summary>
	/// Loads the grid on the sceen
	/// 
	/// </summary>
	void gui_BtnPrepareGrid()
	{
		if(GUILayout.Button("Prepare Grid"))
		{
			Camera.main.GetComponent<EditorCamera>().enabled = true;
			LevelGrid.Instance.CreateBaseGrid(levelGridSize);
			Camera.main.GetComponent<EditorCamera>().SetMinMaxViewPos(levelGridSize);
			changeState(state_OnViewMap);
		}
	}
	
	/// <summary>
	/// Go back to previous state
	/// </summary>
	void gui_BtnBack()
	{
		if(GUILayout.Button("Back"))
		{
			revertState();
			removeElementNotif();
		}
	}
	
	/// <summary>
	/// Delete the selected map
	/// </summary>
	void gui_BtnDeleteMap()
	{
		if(GUILayout.Button("Delete Map"))
		{
			if(mapList.Remove(LevelManager.Instance.MapName))
			{
				LevelManager.Instance.DeleteMap();
				Camera.main.GetComponent<BGManager>().enabled = false;
				mapArray = mapList.ToArray();
				changeState(state_OnRootState);
				guiSetupStack.Clear();
				isMapExisting = (mapList.Count != 0) ? true: false;
			}
		}
	}
	
	/// <summary>
	/// Show the view of the world
	/// </summary>
	void gui_SelBtnWorldView()
	{
		GUILayout.BeginHorizontal();
		if(GUILayout.Button("World 1"))
		{
			LevelGrid.Instance.ActivateWorld(1);
			LevelGrid.Instance.ReprocessBG();
			removeElementNotif();
		}
		
		else if(GUILayout.Button("< >"))
		{
			previewBothWorld(true);
		}
		
		else if(GUILayout.Button("World 2"))
		{
			LevelGrid.Instance.ActivateWorld(2);
			LevelGrid.Instance.ReprocessBG();
			removeElementNotif();
		}
		
		GUILayout.EndHorizontal();		
	}
	
	/// <summary>
	/// Show the options that can be put
	/// on the map
	/// </summary>
	void gui_SelBtnCategories()
	{
		chosenCategory = GUILayout.SelectionGrid(chosenCategory, categories, 4);
		if(chosenCategory != -1)
		{
			removeElementNotif();
			switch(chosenCategory)
			{
				//Cell
				case 0:
					currentCategory = gui_ScrollCellCategory;
					LevelGrid.Instance.ActivateWorld();
					LevelGrid.Instance.ReprocessBG();
					LevelGrid.Instance.ActivateCellCollider(true);
					LevelGrid.Instance.ActivateObjectCollider(false);
					break;
					
				//Theme
				case 1:
					currentCategory = gui_ScrollThemeCategory;
					LevelGrid.Instance.DeactivateWorldView();
					LevelGrid.Instance.BGEdit();
					break;
					
				//Obj
				case 2:
					currentCategory = gui_ScrollObjectCategory;
					LevelGrid.Instance.ActivateWorld();
					LevelGrid.Instance.ReprocessBG();
					LevelGrid.Instance.ActivateCellCollider(false);
					LevelGrid.Instance.ActivateObjectCollider(true);
					break;
					
				//Tut
				case 3:
					currentCategory = gui_ScrollTutCategory;
					LevelGrid.Instance.ActivateWorld();
					LevelGrid.Instance.ReprocessBG();
					LevelGrid.Instance.ActivateCellCollider(false);
					LevelGrid.Instance.ActivateObjectCollider(true);
					break;
			}
			scrollPos = Vector2.zero;
			chosenCategory = -1;	
		}
	}
	
	/// <summary>
	/// Set of maps available for the player
	/// </summary>
	void gui_SelMap()
	{
		if(isMapExisting)
		{
			guiMapName();
			chosenMap = GUILayout.SelectionGrid(chosenMap, mapArray, 2);
			if(chosenMap != -1)
			{
				LevelManager.Instance.MapName = mapArray[chosenMap];
				chosenMap = -1;
				LevelManager.Instance.SelecteMap();
			}
			
			GUILayout.BeginHorizontal();
			gui_BtnDeleteMap();
			gui_BtnLoadMap();
			GUILayout.EndHorizontal();
		}
		
		else
		{
			GUILayout.Box("No Map is Existing");
		}		
	}
	#endregion
	
	#region GUI State Setup
	void state_OnRootState()
	{
		gui_BtnStateChange("Load Map", state_OnLoadMap);
		gui_BtnStateChange("Create Map", state_OnCreateMap);
	}
	
	void state_OnLoadMap()
	{
		gui_SelMap();
		gui_BtnBack();
	}
	
	void state_OnCreateMap()
	{
		guiMapName();	
		gui_SlideMapGrid();	
		gui_BtnPrepareGrid();
		gui_BtnBack();
	}
	
	void state_OnViewMap()
	{
		guiMapName();
		gui_SelBtnWorldView();
		gui_SelBtnCategories();
		currentCategory();
		gui_BtnExportMap();
		gui_BtnBack();
	}
	
	void state_OnWorldPreview()
	{
		if(GUILayout.Button("End Preview"))
		{
			previewBothWorld(false);
		}
	}
	#endregion
	
	void removeElementNotif()
	{
		elementNotif = null;
		LevelGrid.Instance.returnBGPlaneColor();
	}
	
	public void setElementNotif(ILevelElementGUI elementNotif)
	{
		this.elementNotif = elementNotif;
	}
	
	void OnGUI()
	{
		if(isExiting)
			return;

		GUI.skin = levelBuilderSkin;
		
		hideOffSet = 1;
		
		GUILayout.BeginArea(new Rect(0,0, Screen.width * 0.05f, Screen.width * 0.05f));
		
		if(!isViewed)
		{
			isViewed = GUILayout.Toggle(isViewed, GUIContent.none);
			hideOffSet = 0;
		}
		else
		{	
			hideOffSet = 1;
		}
		GUILayout.EndArea();
		
		if(!isViewed)
			return;
		
		GUI.DrawTexture(new Rect(0,0, Screen.width * 0.27f, Screen.height), 
			darkMask, 
			ScaleMode.StretchToFill);
		
		isViewed = GUILayout.Toggle(isViewed, GUIContent.none);
		
		//currentGUIScroll = GUILayout.BeginScrollView(currentGUIScroll, false, false);
		GUILayout.BeginArea(new Rect(Screen.width >> 6,Screen.height * 0.02f, 
			Screen.width * 0.25f, Screen.height * 0.95f));
		
		if(currentGUISetup != null)
		{
			currentGUISetup();	
		}
		GUILayout.Space(Screen.height * 0.03f);
		if(elementNotif != null)
		{
			/*GUILayout.BeginArea(new Rect(Screen.width >> 6,Screen.height * 0.7f, 
			Screen.width * 0.25f, Screen.height * 0.3f));*/
			elementNotifScroll = GUILayout.BeginScrollView(elementNotifScroll, false, false);
			
			if(elementNotif.ToShow())
				elementNotif.ShowGUIElements();
			
			GUILayout.EndScrollView();
			//GUILayout.EndArea();
			//GUILayout.EndScrollView();
		}
		GUILayout.EndArea();
	}
	
	public void Exit()
	{
		Application.LoadLevel("LobbyScene");
	}
	void Update()
	{
		if (Input.GetMouseButtonUp(0))
	    {
	       RaycastHit hitInfo = new RaycastHit();
	       bool hit = Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), 
									  out hitInfo, Mathf.Infinity, 
									  LevelGrid.Instance.ClickableMask);
	       if (hit) 
	       {
				try
				{
	         		elementNotif = (BGPlane2D)hitInfo.transform.gameObject.GetComponent<ReferencePlane2D>().attachedPlane2D;
				}
				catch{}
	       }
	    }
		
		/*
		if(isTransition)
		{
			float now = Time.time - currentTime;
			myVal = Mathf.Min(now/(0.4f), 1.0f);
			if(now >= 0.4f)
			{
				isTransition = false;
			}
			return;
		}*/
	}
}


